##################################################################
# File : /lua/maui/itemlist.lua
# Description : ItemList control
# Author : GPG
##################################################################

local Control = import('control.lua').Control
local Dragger = import('dragger.lua').Dragger

ItemList = Class(moho.item_list_methods, Control) {
	--[[
	Methods:
		SetNewFont(family, pointsize)
		SetNewColors(foreground, background, selected_foreground, selected_background)
		item GetItem(index)
		AddItem('newitem')
		ModifyItem(index, string)
		DeleteItem(index)
		DeleteAllItems()
		index GetSelection()
		SetSelection(index)
		int GetItemCount()
		bool Empty()
		ScrollToTop()
		ScrollToBottom()
		ShowItem(index)
		int GetRowHeight()
	    GetRowHeight()
	    GetStringAdvance()
	    NeedsScrollBar()
	    ShowItem()
	    ShowMouseoverItem()
	    ShowSelection()
	--]]

    __init = function(self, parent, debugname)
        InternalCreateItemList(self, parent)
        if debugname then
            self:SetName(debugname)
        end

        local LazyVar = import('/lua/lazyvar.lua')
        self._font = {_family = LazyVar.Create(), _pointsize = 12}
        self._font._family.OnDirty = function(var)
            self:SetNewFont(self._font._family(), self._font._pointsize)
        end

        self._fg = LazyVar.Create()
        self._fg.OnDirty = function(var)
            self:SetNewColors(var(), nil, nil, nil, nil, nil)
        end

        self._bg = LazyVar.Create()
        self._bg.OnDirty = function(var)
            self:SetNewColors(nil, var(), nil, nil, nil, nil)
        end

        self._sfg = LazyVar.Create()
        self._sfg.OnDirty = function(var)
            self:SetNewColors(nil, nil, var(), nil, nil, nil)
        end

        self._sbg = LazyVar.Create()
        self._sbg.OnDirty = function(var)
            self:SetNewColors(nil, nil, nil, var(), nil, nil)
        end

        self._mofg = LazyVar.Create()
        self._mofg.OnDirty = function(var)
            self:SetNewColors(nil, nil, nil, nil, var(), nil)
        end

        self._mobg = LazyVar.Create()
        self._mobg.OnDirty = function(var)
            self:SetNewColors(nil, nil, nil, nil, nil, var())
        end
    end,

    # lazy var support
    SetFont = function(self, family, pointsize)
        if self._font then
            self._font._pointsize = pointsize
            self._font._family:Set(family)
        end
    end,

    SetColors = function(self, foreground, background, selected_foreground, selected_background, mouseover_foreground, mouseover_background)
        if foreground and self._fg then self._fg:Set(foreground) end
        if background and self._bg then self._bg:Set(background) end
        if selected_foreground and self._sfg then self._sfg:Set(selected_foreground) end
        if selected_background and self._sbg then self._sbg:Set(selected_background) end
        if mouseover_foreground and self._mofg then self._mofg:Set(mouseover_foreground) end
        if mouseover_background and self._mobg then self._mobg:Set(mouseover_background) end
    end,
    
    SetLineColor = function(self, color)
        self._fg:Set(color)
    end,

    OnDestroy = function(self)
        self._font._family:Destroy()
        self._font = nil
        self._fg:Destroy()
        self._fg = nil
        self._bg:Destroy()
        self._bg = nil
        self._sfg:Destroy()
        self._sfg = nil
        self._sbg:Destroy()
        self._sbg = nil
        self._mofg:Destroy()
        self._mofg = nil
        self._mobg:Destroy()
        self._mobg = nil
    end,

    # default override methods, event has the whole event so you can get modifiers
    OnClick = function(self, row, event)
        self:SetSelection(row)
        self:OnSelectionChanged(row)
    end,

    DeleteItem = function(self, index)
    	if self:GetSelection() == index then
    		self:OnSelectionChanged()
    	end
    	moho.item_list_methods.DeleteItem(self, index)
    end,
    
    DeleteAllItems = function(self)
		self:OnSelectionChanged()
    	moho.item_list_methods.DeleteAllItems(self)
    end,
    
    OnDoubleClick = function(self, row, event)
        self:OnClick(row)
        self:OnSelectionChanged(row)
    end,

    # The selection changed via keyboard (up,down,pageup,pagedown,home,end etc)
    OnKeySelect = function(self, row)
    	self:SelectionChanged(row)
    end,
    
    # updated when mouseover item changes, -1 when no mouseover
    OnMouseoverItem = function(self, row)
    end,
    
    OnSelectionChanged = function(self, row)
    end,
}

